﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;


namespace ProgLab.CalendarSyncUtil
{
    public interface ICalendarBook
    {
        string Name { get; }
        string Summary { get; }
        DateTime CreateDate { get; }

        bool AddAppointment(ICalendarItem src);
    }

    public abstract class CalendarBook<TCalendar> : ICalendarBook
    {
        protected TCalendar calendar = default(TCalendar);

        public TCalendar MyCalendar
        {
            get { return calendar; }
        }

        public CalendarBook(TCalendar data)
        {
            calendar = data;
        }

        #region ICalendarBook 成員

        public abstract string Name
        {
            get;
        }

        public abstract string Summary
        {
            get;
        }

        public abstract DateTime CreateDate
        {
            get;
        }

        public abstract bool AddAppointment(ICalendarItem src);

        #endregion
    }

    public class GoogleCalendarBook : CalendarBook<CalendarEntry>
    {
        protected GoogleCalendarSource source = null;

        public GoogleCalendarBook(CalendarEntry data,GoogleCalendarSource source)
            : base(data)
        {
            this.source = source;
        }

        public override string Name
        {
            get { throw new NotImplementedException(); }
        }

        public override string Summary
        {
            get { throw new NotImplementedException(); }
        }

        public override DateTime CreateDate
        {
            get { throw new NotImplementedException(); }
        }

        public override bool AddAppointment(ICalendarItem src)
        {
            if (this.source == null)
                return false;

            EventEntry appointment = new EventEntry();
            appointment.Title.Text = src.Subject;
            appointment.Summary.Text = src.Content;
            Where location = new Where();
            location.ValueString = src.Location;
            appointment.Locations.Add( location );
            When appTime = new When(src.Start, src.End);
            appTime.AllDay = src.IsAllDay;
            appointment.Times.Add(appTime);

            return this.source.AddAppointment(appointment, this.calendar);  
        }
    }
}
